package com.test.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 公平锁
 * 
 * @author xuguangqiao
 *
 */
public class FairLock implements Runnable {
	public static ReentrantLock lock = new ReentrantLock(true);

	public void run() {
		while (true) {
			try {
				lock.lock();
				System.out.println(Thread.currentThread().getName() + "-获得锁");
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} finally {
				lock.unlock();
			}
		}
	}

	public static void main(String[] args) {
		FairLock fairLock = new FairLock();
		Thread t1 = new Thread(fairLock, "Thread_1");
		Thread t2 = new Thread(fairLock, "Thread_2");
		t1.start();
		t2.start();

	}
}
